package com.atom.common.remote.palace;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atom.common.CommonConstant;
import com.atom.common.exception.AvensRemoteDatabaseDaoException;
import com.atom.common.util.BeanToMapUtil;
import com.atom.common.util.CharSetUtil;
import com.atom.common.util.HttpUtil;
import com.atom.common.util.JsonResultParser;
import com.atom.common.util.GlobalConfigReader;
import com.atom.common.util.TicketUtil;

@Service
public class RemoteHistoricTaskDao {
  
  private static Logger logger = Logger.getLogger(RemoteHistoricTaskDao.class);
  
  @Autowired(required = false) HttpSession session;
  
  private static final String HOST = GlobalConfigReader.getStringValue(CommonConstant.PALACE_SERVERNAME);
  
  public void save(RemoteHistoricTaskDO instance) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "done.addDoneTask.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      Map<String, String> remoteHistoricTaskToMap = BeanToMapUtil.toMap(instance);
      params.putAll(remoteHistoricTaskToMap);
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Save RemoteHistoricTaskDO Failed");
    }
  }  

}
